/*
 * 创建日期 2005-10-8
 *
 * 更改所生成文件模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
package com.vanda.report.dp;

import java.math.BigDecimal;
import java.util.List;

import com.vanda.report.ReportException;
import com.vanda.report.ReportGroupRow;
import com.vanda.report.ReportUtils;
import com.vanda.report.Validation;

/**
 * @author wkf5985
 *
 * 更改所生成类型注释的模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
public class SumElement extends AggregationElement {

	public void validator() throws ReportException{
		Validation.checkRequired("SumElement","field",super.getField());
	}
	
	/**
	 * 聚集计算,取总和
	 * @param beanList
	 * @return
	 */
	public Object[] aggregate(List beanList,int start,int end, String[] property)
		throws ReportException{
		Object[] objs = new Object[property.length];
		
		try{
			BigDecimal[] sum = new BigDecimal[property.length];
			for(int i=0;i<sum.length;i++){
				sum[i]= new BigDecimal("0");
			}
			Object[] dataObj = new Object[property.length];
			for(int i=start;i<end;i++){
				Object temp = beanList.get(i);
				
				if(temp instanceof ReportGroupRow){
					continue;
				}
				
				for(int j=0;j<property.length;j++){
					Object value = super.getProperty(temp,property[j]);
					if(dataObj[j] == null){
						dataObj[j] = value;
					}
					BigDecimal bg = ReportUtils.convertBg(value);
					
					if(bg != null)
						sum[j] = sum[j].add(bg);
				}
				
			}
			
			for(int i=0;i<objs.length;i++){
				objs[i] = ReportUtils.convertObj(sum[i],dataObj[i]);
			}
			
		}catch(Exception e){
			throw new ReportException(e);
		}

		
		return objs;
	}
}
